<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>重置密码 - 知更开放平台</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <style>
        body {
            background: #f5f7fa;
            min-height: 100vh;
            display: flex;
            align-items: center;
        }
        .reset-form {
            max-width: 450px;
            margin: 40px auto;
            padding: 30px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.1);
        }
        .platform-name {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }
        .platform-name h1 {
            font-size: 24px;
            margin: 0;
            font-weight: 600;
        }
        .platform-name p {
            color: #666;
            margin: 5px 0 0;
            font-size: 14px;
        }
        .account-info {
            background: #e8f4ff;
            border-radius: 6px;
            padding: 15px;
            margin-bottom: 25px;
        }
        .account-info p {
            margin: 0;
            color: #0056b3;
            font-size: 14px;
        }
        .account-info strong {
            color: #004085;
        }
        .form-label {
            font-weight: 500;
            color: #444;
        }
        .password-requirements {
            font-size: 12px;
            color: #666;
            margin-top: 5px;
        }
        .btn-primary {
            padding: 12px;
            font-weight: 500;
            letter-spacing: 1px;
        }
        .form-control:focus {
            box-shadow: 0 0 0 3px rgba(0,123,255,0.1);
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="reset-form">
            <!-- 平台名称 -->
            <div class="platform-name">
                <h1>{$site_name}</h1>
                <p>安全重置您的账号密码</p>
            </div>

            <!-- 账号信息 -->
            <div class="account-info">
                <p>正在为账号 <strong>{$email}</strong> 重置密码</p>
            </div>
            
            <form id="resetForm" class="needs-validation" novalidate>
                <input type="hidden" name="user_id" value="{$user_id}">
                <input type="hidden" name="code" value="{$code}">
                
                <div class="mb-3">
                    <label for="new_password" class="form-label">新密码</label>
                    <input type="password" 
                           class="form-control" 
                           id="new_password" 
                           name="new_password" 
                           required 
                           minlength="6"
                           placeholder="请输入新密码">
                    <div class="password-requirements">
                        密码长度至少6位，建议使用字母、数字和符号的组合
                    </div>
                    <div class="invalid-feedback">
                        密码长度不能小于6位
                    </div>
                </div>

                <div class="mb-4">
                    <label for="confirm_password" class="form-label">确认新密码</label>
                    <input type="password" 
                           class="form-control" 
                           id="confirm_password" 
                           name="confirm_password" 
                           required
                           placeholder="请再次输入新密码">
                    <div class="invalid-feedback">
                        两次输入的密码不一致
                    </div>
                </div>

                <div class="d-grid">
                    <button type="submit" class="btn btn-primary">确认重置密码</button>
                </div>
            </form>
        </div>
    </div>

    <script src="/static/js/bootstrap.bundle.min.js"></script>
    <script>
        document.getElementById('resetForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            // 表单验证
            if (!this.checkValidity()) {
                e.stopPropagation();
                this.classList.add('was-validated');
                return;
            }

            // 验证密码一致性
            var newPassword = document.getElementById('new_password').value;
            var confirmPassword = document.getElementById('confirm_password').value;
            
            if (newPassword !== confirmPassword) {
                document.getElementById('confirm_password').setCustomValidity('两次输入的密码不一致');
                this.classList.add('was-validated');
                return;
            }

            // 提交表单
            fetch('/user/reset_password', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    user_id: this.elements['user_id'].value,
                    code: this.elements['code'].value,
                    new_password: newPassword,
                    confirm_password: confirmPassword
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 200) {
                    alert('密码重置成功，请使用新密码登录');
                    window.location.href = '/';  // 跳转到首页
                } else {
                    alert(data.message || '密码重置失败');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('密码重置失��，请稍后重试');
            });
        });

        // 密码一致性实时验证
        document.getElementById('confirm_password').addEventListener('input', function() {
            if (this.value !== document.getElementById('new_password').value) {
                this.setCustomValidity('两次输入的密码不一致');
            } else {
                this.setCustomValidity('');
            }
        });
    </script>
</body>
</html> 